package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.constant.RedisConstant;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.LayUITableResult;
import com.situ.mall.util.QiniuUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class ProductServiceImpl implements IProductService {
    private static  final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.SUPPORTS)
    @Override
    public LayUITableResult selectByPage(ProductQuery productQuery) {
        //long begin = System.currentTimeMillis();

        PageHelper.startPage(productQuery.getPage(), productQuery.getLimit());
        List<Product> list = productMapper.selectAll(productQuery);
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        /*long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime > 3000) {
            logger.error("=====执行结束，耗费 {}毫秒=======", takeTime);
        } else if (takeTime > 2000) {
            logger.warn("=====执行结束，耗费 {}毫秒=======", takeTime);
        } else {
            logger.info("=====执行结束，耗费 {}毫秒=======", takeTime);
            //logger.debug("=====执行结束，耗费 {}毫秒=======", takeTime);
        }*/

        return LayUITableResult.ok((int)totalCount, list);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean deleteAll(Integer[] ids) {
        int count = productMapper.deleteAll(ids);
        return count == ids.length;
    }

    @Override
    public boolean deleteById(Integer id) {
        int count = productMapper.deleteByPrimaryKey(id);
        return count == 1;
    }

    @Override
    public boolean add(Product product) {
        int count = productMapper.insertSelective(product);

        //将上传的图片保存到redis中
        String mainImage = product.getMainImage();
        String imageName = mainImage.substring(QiniuUtils.URL.length());
        redisTemplate.opsForSet().add(RedisConstant.UPLOAD_IMAGE_TO_DB, imageName);

        return count == 1;
    }
}
